package com.hemerasolutions.v2trainer.component.search;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ModalityDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ModalitySearchCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalitySearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;


@Component("modalitySearchComp")
@Scope("prototype")
public class ModalitySearchComp extends BaseComponent implements ModalitySearchCompInterface{

	private static final long serialVersionUID = 1L;

	@Autowired
	private ModalityServiceInterface modalityService;
	
	@Autowired
	private ModalityDetailsCompInterface modalityDetails;
	
	private ModalitySearchDelegateInterface delegate;

	private VersionableModality versionableModality;
	
	private List<VersionableModality> foundedVersionableModalitys;
	
	private Date begCreatedAt;
	
	private Date endCreatedAt;
	
	private Date begChangedAt;
	
	private Date endChangedAt;	
	
	private String findByModalityDescription;
	
	private String findByModalityName;
	
	private TrainingType findByTrainingType;	

	private List<TrainingType> findByTrainingTypes;	

	private List<TrainingType> trainingTypes;
	
	@Override
	public void init(){
		super.init();
	}
	
	
	@Override
	public void listAll() {
	   	Map<String,Object> fields = new HashMap<String, Object>();

       	fields.put("librarys", this.getActiveUserAllowedLibrarys());

        this.foundedVersionableModalitys = this.modalityService.searchVersionableModalitys(fields);
        
        if(this.delegate != null){
        	this.delegate.onVersionableModalitySearchCompleted(foundedVersionableModalitys);
        }
	}

	
	@Override
	public void refresh(){
		this.trainingTypes = modalityService.listTrainingTypes();
	}
	
	@Override
	public void versionableModalitySelected(){
		this.delegate.onVersionableModalitySelected(this.versionableModality);
	}
	
	@Override
	public void toViewVersionableModality(){
		this.modalityDetails.setVersionableModality(this.versionableModality);
		this.modalityDetails.setModality(this.versionableModality.getModality());
		this.modalityDetails.refresh();
	}
	
	
	@Override
	public void searchModality(){
    	Map<String,Object> fields = new HashMap<String, Object>();
    	
		if( this.begCreatedAt != null && this.endCreatedAt != null && 
				this.begCreatedAt.compareTo(this.endCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.begChangedAt != null && this.endChangedAt != null && 
			this.begChangedAt.compareTo(this.endChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
    	

    	if ( this.findByModalityName != null && !this.findByModalityName.equals("")) {
            fields.put("name", this.findByModalityName);
        }

    	if ( this.findByModalityDescription != null && !this.findByModalityDescription.equals("")) {
            fields.put("description", this.findByModalityDescription);
        }

    	
    	if (this.findByTrainingTypes != null && !this.findByTrainingTypes.isEmpty()){
    		fields.put("trainingTypes",this.findByTrainingTypes);
    	}


    	if (this.begCreatedAt != null){
    		fields.put("begCreatedAt", this.begCreatedAt);
    	}
    	if (this.endCreatedAt != null){
            fields.put("endCreatedAt",this.endCreatedAt);
        }
        if (this.begChangedAt != null){
        	fields.put("begChangedAt",this.begChangedAt);
        }
        if (this.endChangedAt != null){
        	fields.put("endChangedAt", this.endChangedAt);
        }

       	fields.put("librarys", this.getActiveUserAllowedLibrarys());

        
        this.foundedVersionableModalitys = this.modalityService.searchVersionableModalitys(fields);
	    
		if((this.foundedVersionableModalitys).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.foundedVersionableModalitys.size() + " " + 
									this.localizedText("entrysFounded"));
		}

		if(delegate != null){
			this.delegate.onVersionableModalitySearchCompleted(this.foundedVersionableModalitys);
		}
		
	}



	@Override
	public ModalityServiceInterface getModalityService() {
		return modalityService;
	}



	@Override
	public void setModalityService(ModalityServiceInterface modalityService) {
		this.modalityService = modalityService;
	}



	@Override
	public ModalitySearchDelegateInterface getDelegate() {
		return delegate;
	}



	@Override
	public void setDelegate(ModalitySearchDelegateInterface delegate) {
		this.delegate = delegate;
	}


	@Override
	public List<VersionableModality> getFoundedVersionableModalitys() {
		return foundedVersionableModalitys;
	}



	@Override
	public void setFoundedVersionableModalitys(List<VersionableModality> foundedVersionableModalitys) {
		this.foundedVersionableModalitys = foundedVersionableModalitys;
	}



	@Override
	public Date getBegCreatedAt() {
		return begCreatedAt;
	}



	@Override
	public void setBegCreatedAt(Date begCreatedAt) {
		this.begCreatedAt = begCreatedAt;
	}



	@Override
	public Date getEndCreatedAt() {
		return endCreatedAt;
	}



	@Override
	public void setEndCreatedAt(Date endCreatedAt) {
		this.endCreatedAt = endCreatedAt;
	}



	@Override
	public Date getBegChangedAt() {
		return begChangedAt;
	}



	@Override
	public void setBegChangedAt(Date begChangedAt) {
		this.begChangedAt = begChangedAt;
	}



	@Override
	public Date getEndChangedAt() {
		return endChangedAt;
	}



	@Override
	public void setEndChangedAt(Date endChangedAt) {
		this.endChangedAt = endChangedAt;
	}



	@Override
	public String getFindByModalityDescription() {
		return findByModalityDescription;
	}



	@Override
	public void setFindByModalityDescription(String findByModalityDescription) {
		this.findByModalityDescription = findByModalityDescription;
	}



	@Override
	public String getFindByModalityName() {
		return findByModalityName;
	}



	@Override
	public void setFindByModalityName(String findByModalityName) {
		this.findByModalityName = findByModalityName;
	}



	@Override
	public TrainingType getFindByTrainingType() {
		return findByTrainingType;
	}



	@Override
	public void setFindByTrainingType(TrainingType findByTrainingType) {
		this.findByTrainingType = findByTrainingType;
	}



	@Override
	public List<TrainingType> getFindByTrainingTypes() {
		return findByTrainingTypes;
	}



	@Override
	public void setFindByTrainingTypes(List<TrainingType> findByTrainingTypes) {
		this.findByTrainingTypes = findByTrainingTypes;
	}

	@Override
	public ModalityDetailsCompInterface getModalityDetails() {
		return modalityDetails;
	}

	@Override
	public void setModalityDetails(ModalityDetailsCompInterface modalityDetails) {
		this.modalityDetails = modalityDetails;
	}

	@Override
	public List<TrainingType> getTrainingTypes() {
		return trainingTypes;
	}

	@Override
	public void setTrainingTypes(List<TrainingType> trainingTypes) {
		this.trainingTypes = trainingTypes;
	}


	@Override
	public VersionableModality getVersionableModality() {
		return versionableModality;
	}


	@Override
	public void setVersionableModality(VersionableModality versionableModality) {
		this.versionableModality = versionableModality;
	}
		
	
	
}
